import util.GuiImg;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Alter extends JPanel implements ActionListener {
    /**
     * 改密
     */
    private Icon atmIcon10 = GuiImg.getIcon("/imgs/buttonSure.jpg");
    private JButton btnlogin10 = new JButton(atmIcon10);
    private JPasswordField passw10 = new JPasswordField();
    private JPasswordField passw11 = new JPasswordField();
    private Atm frame;

    public Alter(Atm frame) {
        this.frame = frame;
        this.setLayout(null);
        //旧密码显示框
        JLabel textlabel1 = new JLabel("您的旧密码：");
        textlabel1.setBounds(180, 150, 100, 30);
        this.add(textlabel1);
        //旧密码输入框
        passw10.setBounds(250, 150, 250, 30);
        this.add(passw10);
        //新密码显示框
        JLabel textlabel2 = new JLabel("您的新密码：");
        textlabel2.setBounds(180, 200, 100, 30);
        this.add(textlabel2);
        //新密码输入框
        passw11.setBounds(250, 200, 250, 30);
        this.add(passw11);
        //密码按钮
        this.setOpaque(false);
        btnlogin10.setBounds(330, 300, 100, 40);
        this.add(btnlogin10);
        btnlogin10.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnlogin10) {
            if (frame.getUserArray().get(frame.getIndex()).getPassword().equals(String.valueOf(passw10.getPassword()))){
                frame.getUserArray().get(frame.getIndex()).setPassword(String.valueOf(passw11.getPassword()));
                JOptionPane.showMessageDialog(this, "修改成功");
                frame.itin();
                frame.switchPanel("登录");
            }else{
                JOptionPane.showMessageDialog(this, "原密码错误");
            }
        }
    }

    public JPasswordField getPassw10() {
        return passw10;
    }

    public void setPassw10(JPasswordField passw10) {
        this.passw10 = passw10;
    }

    public JPasswordField getPassw11() {
        return passw11;
    }

    public void setPassw11(JPasswordField passw11) {
        this.passw11 = passw11;
    }
}
